$(function () {
    //全局监听ajax请求，登录失效时跳到不同页面。
    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }
    function timeout(ms) {
        return new Promise(function(resolve) {
            setTimeout(resolve, ms);
        });
    };
    $(document).ajaxStart(function () {
    }).ajaxSend(function() {
        /*var currentUID = getCookie('currentUID');
        var oldUID = window.sessionStorage.getItem('currentUID');
        if (currentUID != oldUID) {
            window.sessionStorage.removeItem("currentUID");
            window.sessionStorage.setItem("currentUID", currentUID);
            showAlert("当前账号已登出，即将切换到新登录账号！");
            timeout(3000);
            window.location.href = '../login/login!oauth2';
            return;
        }*/
    }).ajaxComplete(function (event, request, settings) {
        if (request.responseText != null && request.responseText != undefined) {
            if (request.responseText.indexOf('<title>oauth2登陆接口</title>') > -1) {
                window.location.href = '../login/login!oauth2';
            } else if (request.responseText.indexOf('<title>提供专业电子签名SaaS服务-e签宝</title>') > -1) {
                window.location.href = '../login/login!index';
            }
        }
    });
    showAlert = function (str) {
        if (!str) {
            str = "网络异常，请稍候再试";
        }
        if (str == "token不存在") {
            window.location.href = "../login/login!index";
            return;
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<a class="close" href=javascript:closeAlert();>确认</a>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }
    showAlert_notClose = function (str) {
        if (!str) {
            str = "网络异常，请稍候再试";
        }
        if (str == "token不存在") {
            window.location.href = "../login/login!index";
            return;
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showAlert_nobg = function (str) {
        if (!str) {
            str = "网络异常，请稍候再试";
        }
        if (str == "token不存在") {
            window.location.href = "../login/login!index";
            return;
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<a class="close" href=javascript:closeAlert();>确认</a>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }
    showWaitMeAlert = function (str) {
        if (!str) {
            str = "网络异常，请稍候再试";
        }
        if (str == "token不存在") {
            window.location.href = "../login/login!index";
            return;
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<a class="close" href=javascript:closeWaitMeAlert();>确认</a>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }
    closeWaitMeAlert = function () {
        for (var int = 0; int < 100; int++) {
            if ($("#boht").length > 0) {
                var boht = document.getElementById("boht");
                document.body.removeChild(boht);
            } else {
                break;
            }
        }
        $("#rectangleId div img").click();
    }
    closeAlert = function () {
        for (var int = 0; int < 100; int++) {
            if ($("#boht").length > 0) {
                var boht = document.getElementById("boht");
                document.body.removeChild(boht);
            } else {
                break;
            }
        }
    }

    showblockUI = function () {
        if ($("#boht").length > 0) {
            return;
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info" style="top:350px"><div id="center" style="background:transparent;border: none;">';
        alertHtml += '<img src="../../enterprise/app/images_2.0/loading.gif" style="background: transparent;margin-left: 120px;">';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showFancyboxBlockUI = function () {
        if ($("#boht").length > 0) {
            return;
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg" style="z-index:0;></div>';
        alertHtml += '<div id="info" style="top:350px;"><div id="center" style="background:transparent;border: none;">';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showUrlChoose = function (str, url) {
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<ul id="yn"><li class="yes"><a class="close" href="' + url + '">是</a></li>';
        alertHtml += '<li class="no" onclick="javascript:closeAlert()">否</li></ul>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showUrlMust = function (str, url) {
        if (!str) {
            str = "网络异常，请稍候再试";
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<a class="close" href="' + url + '">确认</a>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showEventChoose = function (str, event) {
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<ul id="yn"><li class="yes" onclick="javascript:closeAlert();' + event + ';">是</li>';
        alertHtml += '<li class="no" onclick="javascript:closeAlert()">否</li></ul>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showEventMust = function (str, event) {
        if (!str) {
            str = "网络异常，请稍候再试";
        }
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += "<a class='close' onclick='javascript:closeAlert();" + event + ";' style='cursor:pointer;'>确认</a>";
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    showChooseType = function (str, url) {
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<ul id="yn"><li class="yes"><a class="close" href="regist!redirect?skip=password&type=1&' + url + '">个人</a></li>';
        alertHtml += '<li class="no"><a class="close" href="regist!redirect?skip=password&type=2&' + url + '">企业</a></li></ul>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }
    showWindowClose = function (str) {
        var boht = document.createElement("span");
        document.body.appendChild(boht);
        boht.id = "boht";
        var alertHtml = '<div id="bg"></div>';
        alertHtml += '<div id="info"><div id="center">';
        alertHtml += '<strong>提示</strong>';
        alertHtml += '<p>' + str + '</p>';
        alertHtml += '<a class="close" href="javascript:closeWin();">确认</a>';
        alertHtml += '</div></div>';
        boht.innerHTML = alertHtml;
    }

    closeWin = function () {
        window.opener = null;
        window.open('', '_self');
        window.close();
    }


    checkStream = function (val) {
        if (!val) {
            return null;
        }

        var json = JSON.parse(val);
        if (null == json) {
            return null;
        }

        if (0 != json.errCode) {
            //showAlert("服务异常:" + json.errCode + "," + json.msg);
            return null;
        }

        return json;
    }
});